<?php 
	class Test_volumes extends CI_Model{

		function __construct(){
			parent::__construct();
		}

		function get_test_volume_list($limit = 0, $offset = 10, $conditions = array()){
            $where = " WHERE 1=1";
            if(!empty($conditions['subject_id'])){
                $where .= " AND tv.subject_id = '{$conditions['subject_id']}' ";
            }

            if(!empty($conditions['type'])){
                $where .= " AND tvp.type = '{$conditions['type']}' ";
            }

            if(!empty($conditions['school_id'])){
                $where .= " AND (tvp.school_id = '{$conditions['school_id']}' OR tvp.school_id = 0) ";
            }

            $sql = "SELECT SQL_CALC_FOUND_ROWS tvp.*, tv.name,tv.id FROM {$this->db->dbprefix}tk_test_volumes_publish AS tvp ";
            $sql .= "LEFT JOIN {$this->db->dbprefix}tk_test_volumes AS tv ON(tvp.volume_id = tv.id) ";
            $sql .= "$where ORDER BY tvp.id DESC LIMIT $limit, $offset ";
            $result = $this->db->query($sql)->result_array();
            $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
            return array("count" => $row_num['row_num'], "result" => $result);
        }

        function get_volume_publish_list($limit = 0, $offset = 0, $conditions = array()){
            $sql = "SELECT * FROM {$this->db->dbprefix}o2o_student where user_id = {$this->user_data['id']} limit 1";
            $student_info = $this->db->query($sql)->row_array();
            $sql = "select class_id FROM edu_o2o_class_student where student_id='{$student_info['student_id']}' ";
            $class_list = $this->db->query($sql)->result_array();
            if(!$class_list){
                return array("count" => 0, "result" => array());
            }
             $where = " WHERE tvp.school_id = '{$this->school_id}' ";
            // $class_ids = array();
            if(count($class_list) > 0){
                $where .= " AND (";
                foreach ($class_list as $key => $value) {
                    if($key == 0){
                        $where .= " FIND_IN_SET('{$value['class_id']}', tvp.classes) ";
                    }else{
                        $where .= " OR FIND_IN_SET('{$value['class_id']}', tvp.classes) ";
                    }
                }
                $where .= " )";
            }
            
            // $class_ids = implode(",", $class_ids);
            
           
            if(!empty($conditions['type'])){
                $where .= " AND tvp.type = '{$conditions['type']}'";
            }
            if(!empty($conditions['subject_id'])){
                $where .= " AND tv.subject_id = '{$conditions['subject_id']}'";
            }
            if(!empty($conditions['home_work_name'])){
                $where .= ' AND tv.name like "%'.$conditions['home_work_name'].'%"';
            }
            $sql = "SELECT SQL_CALC_FOUND_ROWS tvp.id AS pid, tvp.name AS pname, tvp.volume_id, tvp.timeout, tvp.prompt, tv.*, t.teacher_name, s.title FROM {$this->db->dbprefix}tk_test_volumes_publish AS tvp ";
            $sql .= "LEFT JOIN {$this->db->dbprefix}tk_test_volumes AS tv ON(tv.id=tvp.volume_id)";
            $sql .= "LEFT JOIN {$this->db->dbprefix}b_subject AS s ON(s.id=tv.subject_id) ";
            $sql .= "LEFT JOIN {$this->db->dbprefix}o2o_teachers AS t ON(t.id=tvp.teacher_id) ";
            $sql .= "$where ORDER BY tvp.id DESC ";
            if($limit > 0 || $offset > 0){
                $sql .= "LIMIT $limit, $offset";
            }
            $result = $this->db->query($sql)->result_array();
            if($limit > 0 || $offset > 0){
                $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
                return array("count" => $row_num['row_num'], "result" => $result);
            }else{
                return $result;
            }
        }

        function get_volume_publish_history($limit = 0, $offset = 0, $conditions = array()){
            $where = " WHERE home_work_h.uid='{$this->user_data['id']}'";
            if(!empty($conditions['type'])){
                $where .= " AND home_work_h.type = '{$conditions['type']}'";
            }
            
            if(!empty($conditions['home_work_id'])){
                $where .= " AND home_work_h.data_id = '{$conditions['home_work_id']}'";
            }

            if(!empty($conditions['name'])){
                $where .= " AND s.name like '%{$conditions['name']}%'";
            }
            
            $sql = "SELECT SQL_CALC_FOUND_ROWS home_work_h.*, s.name FROM {$this->db->dbprefix}tk_test_volume_historys AS home_work_h ";
            $sql .= "LEFT JOIN {$this->db->dbprefix}o2o_student AS s ON(s.user_id=home_work_h.uid) ";
            $sql .= "$where ORDER BY id DESC LIMIT $limit, $offset ";
            $result = $this->db->query($sql)->result_array();
            $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
            return array("count" => $row_num['row_num'], "result" => $result);
        }
	}